Prac 6:- Decision Tree 
CODE:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.metrics import (accuracy_score, confusion_matrix, precision_score,
                             recall_score, roc_curve, auc)
import matplotlib.pyplot as plt
import numpy as np
filepath = "C:/Users/Admin/Downloads/diabetes.csv"
data = pd.read_csv(filepath)
# Binarize the outcome
data['Outcome'] = data['Outcome'].apply(lambda x: 1 if x >= 0.5 else 0)
X = data.drop(columns=['Outcome'])
y = data['Outcome']
# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize and train the model
clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)
# Get predictions and probabilities (Fix 1: Used predict_proba)
y_pred = clf.predict(X_test)
y_pred_prob = clf.predict_proba(X_test)[:, 1]
# Calculate metrics
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
precision = precision_score(y_test, y_pred)
sensitivity = recall_score(y_test, y_pred)
TN = conf_matrix[0, 0]
FP = conf_matrix[0, 1]
specificity = TN / (TN + FP)
standard_error = np.sqrt((accuracy * (1 - accuracy)) / len(y_test))
# ROC and AUC
fpr, tpr, _ = roc_curve(y_test, y_pred_prob)
roc_auc = auc(fpr, tpr)
# Plot ROC Curve (Fix 2: lw=2 instead of Iw=2)
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()
# Plot Decision Tree (Fix 3: max_depth instead of max_dept)
plt.figure(figsize=(12, 8))
plot_tree(clf, feature_names=X.columns, class_names=['0', '1'], filled=True, max_depth=3)
plt.title("Decision Tree (Max Depth = 3)")
plt.show()
# Print Results
print("Predictions:", y_pred)
print("Accuracy:", accuracy)
print("Confusion Matrix:\n", conf_matrix)
print("Standard Error:", standard_error)
print("Precision:", precision)
print("Sensitivity (Recall):", sensitivity)
print("Specificity:", specificity)
print("AUC:", roc_auc)
OUTPUT:
Outcome…(inferences)
Glucose: This refers to the glucose level used for the initial split in the decision tree, likely representing a numerical value from medical data. It serves as a critical feature in determining the classification outcome.
<= 127.5: This condition compares the glucose level to the threshold of 127.5. If the glucose level is less than or equal to this value, the decision will proceed to the "True" branch. If the value is greater than 127.5, the decision follows the "False" branch.
Gini: The Gini index is a measure of impurity or uncertainty in the decision tree node. A lower Gini value indicates a purer node, where most samples belong to the same class, while a higher Gini value suggests mixed classes.
Samples: This refers to the number of data points that reach a specific node in the decision tree. It helps understand the sample size at each step.
Value: Represents the distribution of classes at the node. For example, a value of [401, 213] indicates that there are 401 instances of one class and 213 of another class, which are the outcome categories (e.g., "No Diabetes" and "Diabetes").
Class: The class represents the predicted outcome for the samples at that node. It is the final classification made by the decision tree.
Color Coding in the Decision Tree:
Orange: Nodes colored in orange represent a classification of "No Diabetes". If a sample reaches an orange node, it is predicted to belong to the "No Diabetes" category.
Blue: Nodes colored in blue represent a classification of "Diabetes". If a sample reaches a blue node, it is predicted to belong to the "Diabetes" category.